from app.controller.add_langgraph_chat_route import add_langgraph_chat_route
from app.model.LgChat import LgChatService
from fastapi import Query
from langchain_core.messages import HumanMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableLambda
from langserve import add_routes

from app.config.env import env
from app.controller.add_langgraph_approve_route import add_langgraph_approve_route
from app.controller.add_langgraph_route import add_langgraph_route
from app.controller.add_lg_approve_route import add_lg_approve_route
from app.controller.add_sqlmodel_route import add_sqlmodel_route
from app.controller.add_user_route import add_user_route
from app.controller.custom_chat_playground import add_custom_chat_playground_route
from app.controller.custom_stream_api import add_custom_stream_api_route
from app.controller.test_connection import add_test_connection_route
from app.controller.test_sqlmodel import add_test_sqlmodel_route
from app.controller.test_sync import add_test_sync_route
from app.controller.translate_controller import add_translate_route
from app.create_app import create_app
from app.middlewares.app_middlewares import add_app_middlewares
from app.model.LgApprove import LgApproveService
from app.model.LgMessage import LgMessageService
from app.model.LlmOrder import LlmOrderService
from app.model.LlmProduct import LlmProductService
from app.run_uvicorn import run_uvicorn
from app.utils.ModelInputSchema import ModelInputSchema
from app.utils.add_async_route import add_async_route
from app.utils.llm_utils import create_llm
from app.utils.next_id import add_next_id_route

app = create_app()

add_app_middlewares(app)

add_translate_route(app)
add_custom_chat_playground_route(app)
add_test_sync_route(app)
add_custom_stream_api_route(app)
add_test_connection_route(app)
add_test_sqlmodel_route(app)
add_next_id_route(app)
add_sqlmodel_route(app)
add_user_route(app)
add_langgraph_route(app)
add_lg_approve_route(app)
add_langgraph_approve_route(app)
add_langgraph_chat_route(app)


@app.get("/get_env")
async def test():
  return env.model_dump_json()


@app.get("/test")
async def test():
  return {"msg": "hello"}


@app.get("/test_llm")
async def test_llm(user_content: str = Query(..., description="用户输入的文本内容，将传递给大语言模型处理")):
  return (create_llm() | StrOutputParser()).invoke([HumanMessage(content=user_content)])


add_routes(
  app=app,
  runnable=RunnableLambda(lambda x: x['messages']) | create_llm() | StrOutputParser(),
  input_type=ModelInputSchema,
  path="/doubao"
)

add_async_route(
  app=app,
  runnable=RunnableLambda(lambda x: x['messages']) | create_llm("bailian-qwen-turbo").with_types(input_type=ModelInputSchema),
  path="/qwen"
)

LlmOrderService.add_route(app=app, path="/llm_order")
LlmProductService.add_route(app=app, path="/llm_product")
LgApproveService.add_route(app=app, path="/lg_approve")
LgMessageService.add_route(app=app, path="/lg_message")
LgChatService.add_route(app=app, path="/lg_chat")

if __name__ == "__main__":
  run_uvicorn()
